package com.rcloud.stock.adapter;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.rcloud.stock.R;
import com.rcloud.stock.bean.Gg;

public class DetailGgRecyclerAdapter extends ListAdapter<Gg, DetailGgRecyclerAdapter.ViewHolder> {
    public DetailGgRecyclerAdapter() {
        super(new DiffUtil.ItemCallback<Gg>() {
            @Override
            public boolean areItemsTheSame(@NonNull Gg oldItem, @NonNull Gg newItem) {
                return oldItem.getId().equals(newItem.getId());
            }

            @Override
            public boolean areContentsTheSame(@NonNull Gg oldItem, @NonNull Gg newItem) {
                return oldItem.getCode().equals(newItem.getCode());
            }
        });
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_news,parent,false);
        ViewHolder holder = new ViewHolder(view);
        holder.itemView.setOnClickListener(v -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("gg",(Gg) v.getTag(R.integer.history_tag));
            NavController controller = Navigation.findNavController(v);
            controller.navigate(R.id.articleFragment,bundle);
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Gg gg = (Gg) getItem(position);
        holder.itemView.setTag(R.integer.history_tag,gg);
        holder.titleTv.setText(gg.getTitle());
        holder.authorTv.setText(gg.getAuthor());
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView titleTv;
        private final TextView authorTv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            titleTv = itemView.findViewById(R.id.titleTv);
            authorTv = itemView.findViewById(R.id.authorTv);
        }
    }
}
